import { asyncTasks } from 'roc-utils'
import db from '../app/database.js'
import api from '../utils/apiBase.js'

class UserService {
    /**
     * 获取用户信息
     * @param {number} userId
     * @returns {Promise<{code: number, data: *, message: string}|{code: number, data: string, message: string}|void>}
     */
    async getUserById(userId) {
        const sql = `SELECT id,username,nickname,avatar,add_time,update_time FROM roc_user WHERE id = ?;`
        const [err, results] = await asyncTasks(db.query(sql, [userId]))
        if (err) return console.log('sql错误:', err.sqlMessage)
        const users = results[0]
        if (users.length) {
            return api.success(users[0])
        } else {
            return api.error('无此用户')
        }
    }
}

export default new UserService()
